<!-- 字典管理 -->
<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { Plus as IconPlus } from '@element-plus/icons-vue'
import type { IDbAction, IDbSearchConfig, IDbTable } from '@/types/global'
import useNotic from '@/hooks/useNotic'
import DictInfo from './comps/DictInfo.vue'

// 搜索栏配置
const searchConfig = reactive<IDbSearchConfig[]>([{ label: '字典名称', prop: '', type: 'input' }])

// 表格数据
const tableData = reactive<IDbTable>({
  columns: [
    { label: '字典编号', prop: '', minWidth: '100px' },
    { label: '字典名称', prop: '', minWidth: '100px' },
    { label: '字典类型', prop: '', minWidth: '100px' },
    { label: '备注', prop: '', minWidth: '100px' },
    { label: '操作', prop: 'handle', fixed: 'right', type: 'slot', width: '150px' }
  ],
  data: [],
  total: 0,
  search: {
    pageNum: 1,
    pageSize: 20
  }
})

// 弹窗数据
const dictInfoRef = ref<InstanceType<typeof DictInfo>>()
const dialogData = reactive({
  title: '', // 弹窗标题
  disabled: false // 是否禁用
})

getDataList()

/**
 * 获取表格数据
 */
function getDataList() {}
/**
 * 点击添加
 */
function addClick() {
  dialogData.title = '添加字典'
  dialogData.disabled = false
  dictInfoRef.value?.init()
}
/**
 * 点击详情/编辑
 */
function infoClick(row: any, disabled: boolean) {
  dialogData.title = disabled ? '字典详情' : '编辑字典'
  dialogData.disabled = disabled
  dictInfoRef.value?.init()
}
/**
 * 点击删除
 */
function delClick(scoped: any) {
  useNotic().confirm(`您确定要删除第${scoped.$index + 1}个字典吗？`, (flag) => {
    console.log(flag)
  })
}
/**
 * 设置表格按钮
 */
function setBtnList(scoped: any) {
  const list: IDbAction[] = [
    { label: '详情', type: 'primary', show: true, onClick: () => infoClick(scoped.row, true) },
    { label: '编辑', type: 'primary', show: true, onClick: () => infoClick(scoped.row, false) },
    { label: '删除', type: 'danger', show: true, onClick: () => delClick(scoped) }
  ]
  return list
}
</script>

<template>
  <dui-page :search-config="searchConfig" :search-data="tableData.search" @search="getDataList">
    <template #btn>
      <el-button type="primary" :icon="IconPlus" @click="addClick">添加字典</el-button>
    </template>
    <template #default>
      <dui-table
        :data="tableData.data"
        :columns="tableData.columns"
        :total="tableData.total"
        :search="tableData.search"
        @search="getDataList"
      >
        <template #handleSlot="scoped">
          <dui-action :list="setBtnList(scoped)" />
        </template>
      </dui-table>
      <!-- 详情弹窗 -->
      <DictInfo ref="dictInfoRef" v-bind="dialogData" />
    </template>
  </dui-page>
</template>
